<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * 后台管理左侧菜单
 * @author 		dd
 * @qq                                  451963230
 * @email		451963230@qq.com
 */
class ProductList extends Admin_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('JayphaDB');
        if (is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate))
        {
            $this->adminCurrentTemplate = 'default';
        }
        $this->dbTablePrefix = $this->config->item('db_table_prefix');
    }

    function index()
    {
        parse_str($this->input->server('QUERY_STRING'), $this->input->get());
        if (!$this->noLoginErr())
            return false;
        $data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
        $tvName = "products";
        if ($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $active = $this->input->post('active');
            $del = $this->input->post('del');
            $id = $this->input->post('id');
            if (is_array($id))
            {
                $id = implode(',', $id);
            }
            $data['disabled'] = $this->input->post('disabled');
            $data['note_id'] = $this->input->post('note_id');
            $data['userName'] = $this->input->post('userName');
            $data['createTimeStart'] = $this->input->post('createTimeStart');
            $data['createTimeEnd'] = $this->input->post('createTimeEnd');
        } else
        {
            $data['disabled'] = $this->input->get('disabled');
            $data['note_id'] = $this->input->get('note_id');
            $data['userName'] = $this->input->get('userName');
            $data['createTimeStart'] = $this->input->get('createTimeStart');
            $data['createTimeEnd'] = $this->input->get('createTimeEnd');
        }
        $data['userName'] = urldecode($data['userName']);

        $condition = array();
        $db_like_key = array();
        $db_like_value = array();
        if (strlen($data['disabled'] > 0))
        {
            $condition['disabled'] = $data['disabled'];
        }
        if (strlen($data['note_id']) > 0)
        {
            array_push($db_like_key, 'note_id');
            array_push($db_like_value, strtoupper($data['note_id']));
        }
        if (strlen($data['userName']) > 0)
        {
            array_push($db_like_key, 'userName');
            array_push($db_like_value, $data['userName']);
        }
        if (strlen($data['createTimeStart']) > 0)
        {
            $condition['createTime >= '] = $data['createTimeStart'];
        }
        if (strlen($data['createTimeEnd']) > 0)
        {
            $condition['createTime <= '] = date('Y-m-d', strtotime($data['createTimeEnd']) + 24 * 60 * 60);
        }
        if (count($db_like_key) > 0)
        {
            $condition['db_like'] = array_combine($db_like_key, $db_like_value);
        }

        unset($db_like_key);
        unset($db_like_value);
        $where = $condition;

        if ($this->input->server('REQUEST_METHOD') == 'POST')
        {
            switch (strtoupper($active))
            {
                case 'DEL':
                    //删除
                    if (strlen($del) == 0)
                    {
                        //删除所选
                        $condition['db_where_in'] = array('id', $id);
                    }
                    $this->JayphaDB->delDB($this->dbTablePrefix . $tvName, $condition);
                    break;
                case 'REMOVE':
                    //移动
                    $condition2['db_where_in'] = array('id', $id);
                    $note_id = $this->input->post('note_id');
                    $this->JayphaDB->updateDB(array('note_id' => $note_id), $this->dbTablePrefix . $tvName, $condition2);
                    break;
                case 'DISABLED':
                    //启用状态
                    $condition2['db_where_in'] = array('id', $id);
                    $disabled = $this->input->post('disabled');
                    $this->JayphaDB->updateDB(array('disabled' => $disabled), $this->dbTablePrefix . $tvName, $condition2);
                    break;
                case 'NEWEST':
                    //最新状态
                    $condition2['db_where_in'] = array('id', $id);
                    $newest = $this->input->post('newest');
                    $this->JayphaDB->updateDB(array('newest' => $newest), $this->dbTablePrefix . $tvName, $condition2);
                    break;
                case 'TOP':
                    //置顶状态
                    $condition2['db_where_in'] = array('id', $id);
                    $top = $this->input->post('top');
                    $this->JayphaDB->updateDB(array('top' => $top), $this->dbTablePrefix . $tvName, $condition2);
                    break;
                case 'RECOMMEND':
                    //推荐状态
                    $condition2['db_where_in'] = array('id', $id);
                    $recommend = $this->input->post('recommend');
                    $this->JayphaDB->updateDB(array('recommend' => $recommend), $this->dbTablePrefix . $tvName, $condition2);
                    break;
                case 'SHOWHOME':
                    //首页显示状态
                    $condition2['db_where_in'] = array('id', $id);
                    $showhome = $this->input->post('showhome');
                    $this->JayphaDB->updateDB(array('showhome' => $showhome), $this->dbTablePrefix . $tvName, $condition2);
                    break;
            }
        }

        unset($condition);

        $config['base_url'] = site_url("admin/adminList/index");
        $config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix . $tvName, $where);
        $config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
        $config['uri_segment'] = 4;
        $config['num_links'] = 5;
        //$config['full_tag_open'] = '<div>';
        //$config['full_tag_close'] = '</div>';
        $config['first_link'] = '|<';
        $config['last_link'] = '>|';
        $config['next_link'] = '>>';
        $config['prev_link'] = '<<';

        $this->pagination->initialize($config);

        $data['currentSite'] = '产品管理<span class="split">>></span>产品列表';
        $adminGroupArr = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0, 0, 'id,parentId,title', $this->dbTablePrefix . 'admin_group', '', 'parentId,id', false));
        $data['adminGroupAll'] = $this->JayphaDB->getChildrenList($adminGroupArr, 0, 1);
        $data['phpSelf'] = $config['base_url'];
        $data['pageTag'] = '产品管理';
        $data['editPage'] = site_url('admin/productList/edit');
        $data['editTitle'] = '修改产品';
        $data['addTitle'] = '添加产品';
        $data['perm'] = 'admin';
        $data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
        $data['tvName'] = $tvName;
        $order = 'note_id,id';
        $num = $this->uri->segment(4, 0);
        $data['adminList'] = $this->JayphaDB->getDB($config['per_page'], $num, '', $this->dbTablePrefix . $tvName, $where, $order, false);
        foreach ($data['adminList'] as $adminItem)
        {
            $adminItem->groupTitle = $this->JayphaDB->getDBValue('title', $this->dbTablePrefix . 'admin_group', array('id' => $adminItem->note_id), '');
        }
        $this->load->view("admin/$this->adminCurrentTemplate/productList", $data);
    }

    function edit()
    {
        if (!$this->noLoginErr())
            return false;
        $data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
        $tvName = "products";
        $data['tvName'] = $tvName;
        $data['perm'] = 'product';
        if ($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $data = $this->JayphaDB->create($this->input->post(), $tvName);
            $updateData = $data;
            $data['type'] = "img";
            $data['returnInput'] = $this->input->get_post('returnInput');

            switch (strtolower($data['type']))
            {
                case 'img':
                    $config['upload_path'] = './uploadFiles/images/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    break;
                case 'media':
                    $config['upload_path'] = './uploadFiles/flash/';
                    $config['allowed_types'] = 'flv|swf|mp4|avi|wmv|3g';
                    break;
                default:
                    $config['upload_path'] = './uploadFiles/files/';
                    $config['allowed_types'] = 'doc|docx|ppt|rar|zip|txt';
                    break;
            }
            $data['upload_path'] = $config['upload_path'];
            $config['max_size'] = '3000';
            $config['encrypt_name'] = true;
            $config['remove_spaces'] = true;
            $this->load->library('upload', $config);
            $field_name = 'img';
            if (!$this->upload->do_upload($field_name))
            {
                $data['error'] = $this->upload->display_errors();
                $updateData['img'] = $data['error'];
            } else
            {
                $data['imgs'] = $this->upload->data();
                $updateData['img'] = $data['upload_path'] . $data['imgs']['file_name'];
            }
            if (strlen($data['id']) == 0 or $data['id'] == 0)
            {
                //添加
                if ($this->JayphaDB->getDBCount($this->dbTablePrefix . $tvName, array('upper(name)' => strtoupper($data['name']))) > 0)
                {
                    $data['errorScript'] = array('lhgdialog_alert_mess' => array(
                            'width' => 360,
                            'height' => 108,
                            'title' => '产品错误',
                            'content' => '<span class="red">对不起，产品名称已经存在，请选择其它产品名称！</span>',
                            'url' => 'history.back()',
                            'target' => '_self',
                            'conver' => true,
                            'resize' => false,
                            'btns' => true,
                            'btnText' => '返回',
                            'rightClose' => false
                            ));
                    $this->load->view("admin/$this->adminCurrentTemplate/error", $data);
                    return false;
                }


                $updateData['createTime'] = date('Y-m-d H:i:s', time());
                $this->JayphaDB->insertDB($this->dbTablePrefix . $tvName, $updateData, true);
                unset($data['errorScript']['lhgdialog_alert_mess']);
                $data['errorScript'] = array('lhgdialog_alert_mess' => array(
                        'width' => 240,
                        'height' => 108,
                        'title' => '添加成功 ',
                        'content' => '您已成功添加产品！',
                        'url' => 'location.reload',
                        'target' => '_self',
                        'conver' => true,
                        'resize' => false,
                        'drag' => false,
                        'btns' => true,
                        'btnText' => '关闭',
                        'rightClose' => false
                        ));
                $this->load->view("admin/$this->adminCurrentTemplate/error", $data);
                return false;
            } else
            {
                //修改
                $where = array('id' => $data['id']);
                unset($updateData['id']);
                $this->JayphaDB->updateDB($updateData, $this->dbTablePrefix . $tvName, $where);
                unset($data['errorScript']['lhgdialog_alert_mess']);
                $data['errorScript'] = array('lhgdialog_alert_mess' => array(
                        'width' => 260,
                        'height' => 108,
                        'title' => '修改成功 ',
                        'content' => '您已成功修改产品相关信息！',
                        'url' => 'location.reload',
                        'target' => '_self',
                        'conver' => true,
                        'resize' => false,
                        'drag' => false,
                        'btns' => true,
                        'btnText' => '关闭',
                        'rightClose' => false
                        ));
                $this->load->view("admin/$this->adminCurrentTemplate/error", $data);
                return false;
            }
        } else
        {
            $data['id'] = $this->uri->segment(4);
            if (strlen($data['id']) == 0)
            {
                $data['id'] = 0;
            } else
            {
                $productLists = $this->JayphaDB->getDB(0, 1, '', $this->dbTablePrefix . $tvName, array('id' => $data['id']), '', false);
//                        var_dump($productLists);
                if (count($productLists) != 0)
                {
                    foreach ($productLists[0] as $key => $row)
                    {
                        $data[$key] = $row;
                    }
                } else
                {
                    $data['id'] = 0;
                }
            }
        }
//                        var_dump($data);exit;
        $adminGroupAll = $this->JayphaDB->objToArr($this->JayphaDB->getDB(0, 0, 'id,parentId,title', $this->dbTablePrefix . 'admin_group', array('disabled' => 0), 'parentId,id', false));
        $data['adminGroupAll'] = $this->JayphaDB->getChildrenList($adminGroupAll, 0, 0);
        $data['note'] = $this->JayphaDB->getDB(0, 0, '', $this->dbTablePrefix . 'notes', '', '', false);
//        var_dump($data['adminGroupAll'] );exit;
        $this->load->view("admin/$this->adminCurrentTemplate/productEdit", $data);
    }

}

/* End of file productLIst.php */
/* Location: ./application/controllers/admin/productLIst.php */